package com.zhugang.week01;

/**
 * @program algorithms
 * @description: reverseLeftWord
 * @author: chanzhugang
 * @create: 2022/03/23 22:56
 */
public class ReverseLeftWord {

    public static void main(String[] args) {
        ReverseLeftWord reverseLeftWord = new ReverseLeftWord();
        String str = "abcdefg";
        String result = reverseLeftWord.reverseLeft(str, 2);
        System.out.println(result);
    }

    /**
     * 左旋转字符串:
     */
    public String reverseLeft(String s, int k) {
        // 把 0 - k 往后挪
        int n = s.length();
        char[] temp = new char[n];
        for (int i = 0; i < k; i++) {
            temp[i + (n - k)] = s.charAt(i);
        }
        // 把 k - s.length 往前挪
        for (int i = k; i < n; i++) {
            temp[i - k] = s.charAt(i);
        }
        return new String(temp);
    }
}